package com.nfit.shopadmin.configure;

import com.nfit.shopadmin.Interceptor.AdminLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * ClassName:WebMvcConfiguration
 * Package:com.nfit.shopweb.configure
 * Description
 *配置类,处理拦截器,跨域配置
 * @Author L
 * @Create 2024/11/15 19:02
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private AdminLoginInterceptor loginInterceptor;

    /**
     * 拦截器注册方法
     * @param registry 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(loginInterceptor)
                //excludePathPatterns() 那些路径不拦截
                .excludePathPatterns("/admin/user/wechatLoginPhone",
                        "/admin/product/**","/admin/productSku/**","/admin/category/**")
                //addPathPatterns() 那些路径需要拦截 ,/** 拦截所有路径
                .addPathPatterns("/**");
    }

    //处理跨域问题
    @Override
    public void addCorsMappings(CorsRegistry corsRegistry){

        corsRegistry.addMapping("/**")      // 添加路径规则
                .allowCredentials(true)               // 是否允许在跨域的情况下传递Cookie
                .allowedOriginPatterns("*")           // 允许请求来源的域规则
                .allowedMethods("*")
                .allowedHeaders("*") ;                // 允许所有的请求头
    }


}
